﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MSUS.Kinect.SkeletonTracker;
using NUI = Microsoft.Research.Kinect.Nui;
using Microsoft.Win32;
using System.IO;
using MSUS.Kinect.GestureEditor.ViewModels;

namespace MSUS.Kinect.GestureEditor
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			GesturePath.Text = Properties.Settings.Default.LastGestureFile;
		}

		private MainViewModel model;
		private JointPositionHistory gesture;

		private void OpenGesture_Click(object sender, RoutedEventArgs e)
		{
			JointPositionHistory newGesture = JointPositionHistory.LoadPositionHistory(GesturePath.Text);
			if (newGesture != null)
			{
				gesture = newGesture;

				model = new MainViewModel();
				model.LoadVectors(gesture, 640, 480);
				DataContext = model;
	
				RenderGesture();
			}
		}

		private void RenderGesture()
		{
			//gesture.RenderPoints(MainCanvas, 640, 480, 0);
		}

		private void FindGestureFile_Click(object sender, RoutedEventArgs e)
		{
			string lastDataFile = Properties.Settings.Default.LastGestureFile;
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Gesture files (*.ges)|*.ges|All files (*.*)|*.*";
			dlg.FilterIndex = 1;
			if (File.Exists(lastDataFile))
				dlg.FileName = lastDataFile;

			dlg.ShowDialog();

			GesturePath.Text = dlg.FileName;
		}

		private void GesturePath_TextChanged(object sender, TextChangedEventArgs e)
		{
			string file = GesturePath.Text;
			if (string.IsNullOrWhiteSpace(file) || File.Exists(file))
			{
				Properties.Settings.Default.LastGestureFile = file;
				Properties.Settings.Default.Save();
			}
		}

		private void SaveGesture_Click(object sender, RoutedEventArgs e)
		{
			if (gesture != null)
			{
				gesture.StorePositionHistory(GesturePath.Text, gesture.NeckVector, "");
			}
		}
	}
}
